#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import threading


class DataQueue(object):
    def __init__(self):
        self.__sem_lock = threading.Semaphore(1)
        self.__wait_buffer = []

    def set_data(self, data):
        try:
            self.__sem_lock.acquire()
            self.__wait_buffer[0] = data
        finally:
            self.__sem_lock.release()

    def get_data(self):
        try:
            self.__sem_lock.acquire()
            return self.__wait_buffer.pop(0)
        finally:
            self.__sem_lock.release()

    def append(self, data):
        try:
            self.__sem_lock.acquire()
            if self.len() == 0:
                self.__wait_buffer.append(data)
            else:
                self.__wait_buffer[0] = data
        finally:
            self.__sem_lock.release()

    def pop(self):
        try:
            self.__sem_lock.acquire()
            if self.len() > 0:
                return self.__wait_buffer.pop(0)
        finally:
            self.__sem_lock.release()

    def len(self):
        return len(self.__wait_buffer)
